import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_shoes/define/define_colors.dart';
import 'package:smart_shoes/module/map/map.dart';
import 'package:smart_shoes/module/my/mine.dart';

/// 创建底部导航栏单项的工具函数
/// 提升代码复用性，方便统一管理导航栏项的样式
BottomNavigationBarItem _createBottomNavigationBarItem({
  required String checked,
  required String unchecked,
  required String label,
  required Color backgroundColor,
}) {
  return BottomNavigationBarItem(
    icon: Image.asset(checked),
    activeIcon: Image.asset(unchecked),
    label: label,
    backgroundColor: backgroundColor,
  );
}

/// 主页组件，继承自 StatefulWidget
class HomePage extends StatefulWidget {
  /// 构造函数，接收一个可选的 key 参数
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

/// 主页组件的状态类
class _HomePageState extends State<HomePage> {
  /// 存储不同页面的列表，使用 final 保证不可变性
  final List<Widget> pages;

  /// 当前选中的底部导航栏索引
  int _currentIndex = 0;

  /// 构造函数初始化页面列表
  _HomePageState()
    : pages = [
        // 避免循环引用，这里用占位组件代替
        const MapPage(),
        const MyPage(), const MyPage(), const MyPage(), const MyPage(),
        const MyPage(), const MyPage(), const MyPage(),
      ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: pages[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed, // 设置类型为 fixed
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },

        selectedItemColor: CustomColors.buttonColor, // 选中时的标签颜色
        unselectedItemColor: Colors.grey, // 未选中时的标签颜色
        showSelectedLabels: true, // 显示选中标签
        showUnselectedLabels: true, // 显示未选中标签
        items: [
          _createBottomNavigationBarItem(
            checked: 'assets/images/ic_home_checked.png',
            unchecked: 'assets/images/icon_back_tone.png',
            label: '首页',
            backgroundColor: CustomColors.backgroundColor,
          ),
          _createBottomNavigationBarItem(
            checked: 'assets/images/icon_back_sensor.png',
            unchecked: 'assets/images/icon_back_sensor.png',
            label: '传感器',
            backgroundColor: CustomColors.backgroundColor,
          ),
          _createBottomNavigationBarItem(
            checked: 'assets/images/icon_back_message.png',
            unchecked: 'assets/images/icon_back_message_s.png',
            label: '消息',
            backgroundColor: CustomColors.backgroundColor,
          ),
          _createBottomNavigationBarItem(
            checked: 'assets/images/touxiang.png',
            unchecked: 'assets/images/touxiang.png',
            label: '我的',
            backgroundColor: CustomColors.backgroundColor,
          ),
        ],
      ),
    );
  }
}
